import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class DivideWeek {
	Calendar calendar;
	int numOfDays;
	int first;
	int last;
	
	public DivideWeek(int year, int month) {
		this.calendar = Calendar.getInstance();
		calendar.set(Calendar.YEAR, year);
		calendar.set(Calendar.MONTH, month-1);
		numOfDays = calendar.getActualMaximum(Calendar.DATE);
		calendar.set(Calendar.DATE, 1);
		first = calendar.get(Calendar.DAY_OF_WEEK);
		calendar.set(Calendar.DATE, numOfDays);
		last = calendar.get(Calendar.DAY_OF_WEEK);
	}
	
	public List<List<Integer>> divide() {
		List<Integer> firstWeek = new ArrayList<Integer>();
		List<Integer> secondWeek = new ArrayList<Integer>();
		List<Integer> thirdWeek = new ArrayList<Integer>();
		List<Integer> fourthWeek = new ArrayList<Integer>();
		if (8-first + last < 7) {
			int i = 1;
			for (; i <= 8 - first + 7; i++) firstWeek.add(i);
			for (int j = 0; j < 7; j++) {
				secondWeek.add(i);
				i++;
			}
			for (int j = 0; j < 7; j++) {
				thirdWeek.add(i);
				i++;
			}
			for (; i <= numOfDays; i++) fourthWeek.add(i);
		}
		else if (8-first > last) {
			int i = 1;
			for (; i <= 8 - first; i++) firstWeek.add(i);
			for (int j = 0; j < 7; j++) {
				secondWeek.add(i);
				i++;
			}
			for (int j = 0; j < 7; j++) {
				thirdWeek.add(i);
				i++;
			}
			for (; i <= numOfDays; i++) fourthWeek.add(i);
		}
		else {
			int i = 1;
			for (; i <= 15 - first; i++) firstWeek.add(i);
			for (int j = 0; j < 7; j++) {
				secondWeek.add(i);
				i++;
			}
			for (int j = 0; j < 7; j++) {
				thirdWeek.add(i);
				i++;
			}
			for (; i <= numOfDays; i++) fourthWeek.add(i);
		}
		List<List<Integer>> result = new ArrayList<List<Integer>>();
		result.add(firstWeek);
		result.add(secondWeek);
		result.add(thirdWeek);
		result.add(fourthWeek);
		return result;
	}

}
